﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using SIIMWeb.YIBAO;
using System.Configuration;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace TradeTest
{
    public partial class FormTrade : Form
    {
        public FormTrade()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }
        DataRead read = new DataRead();
        private void FormTrade_Load(object sender, EventArgs e)
        {
            int i = 0;
            Text = Text + "[地址:" + ConfigurationManager.AppSettings["SIIMURL"] + "]";
            toolStripTextBox1.Text = ConfigurationManager.AppSettings["SIIMURL"].Replace("tcp://", "").Replace(":20000", "");
            cbxbxlbxx.DisplayMember = "YLBXMC";
            cbxbxlbxx.ValueMember = "HZSRM3";
            cbxbxlbxx.DataSource = read.BXLBXX();
            cbxbxlbxx.SelectedIndex = 0;
            //SIIMClient.run();
        }
        int count = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                try
                {
                    for (int i = 0; i < numericUpDown1.Value; i++)
                    {
                        ThreadPool.QueueUserWorkItem(o => sendcall(o));
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("请选择要调用的交易");
            }
        }


        public void sendcall(object o)
        {
            try
            {
                var ylbxid = cbxbxlbxx.SelectedValue.ToString();
                var siimurl = "tcp://" + toolStripTextBox1.Text + ":20000";
                var filename = listView1.SelectedItems[0].Tag.ToString();
                toolStripLabel1.Text = "处理中: " + (count++);
                var str = YIBAOJK.ZMQCallF(ylbxid, listView1.SelectedItems[0].Text, File.ReadAllText(filename, Encoding.UTF8), siimurl);

                if (numericUpDown1.Value == 1 && checkBox1.Checked)
                {
                    Process.Start(str);
                }
                else
                {
                    if (numericUpDown1.Value == 1)
                    {
                        new FormOutCS(str).ShowDialog();
                    }
                }
                toolStripLabel1.Text = "处理中: " + (count--);
            }
            catch (Exception)
            {
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            loadin();
        }

        private void cbxbxlbxx_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadin();
        }

        public void loadin()
        {
            listView1.Items.Clear();
            var path = Application.StartupPath + "\\" + cbxbxlbxx.SelectedValue;
            if (!File.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            foreach (var item in Directory.GetFiles(path, "*_IN.xml"))
            {
                var viewitem = new ListViewItem(Path.GetFileNameWithoutExtension(item).ToUpper().Replace("_IN", ""));
                viewitem.Tag = item;
                viewitem.SubItems.Add(item);
                listView1.Items.Add(viewitem);
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            toolStripTextBox1.Text = ConfigurationManager.AppSettings["SIIMURL"];
        }

        private void listView1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                button1_Click(sender, e);
            }
        }

        private void 打开文件位置ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            Process.Start(Path.GetDirectoryName(listView1.SelectedItems[0].Tag.ToString()));
        }

        private void toolStripButton1_Click_1(object sender, EventArgs e)
        {
            new FormZIFUBL(cbxbxlbxx.SelectedValue.ToString(), (cbxbxlbxx.SelectedItem as DataRowView)["ylbxmc"].ToString());
        }
    }
}
